<?php

/*************************************************************************
Class Pick_Core_LanguagesOther
----------------------------------------------------------------
PICKCO CMS Project
Company: PICKCO  Co., Ltd  
Email: admin@pick.vn
Last updated: 15/07/2008
**************************************************************************/
class Pick_Core_LanguagesOther extends Pick_Core_Objects {
	
	function Pick_Core_LanguagesOther($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->objectInfo 	= 'Pick_Core_LanguagesOtherInfo';
		$this->table = DB_PREFIX."languages_other";
	}
/*-----------------------------------------------------------------------*
* Function: getObjects
* Parameter: WHERE $key
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromTwoTable($page = 1, $condition = array(), $sort = array(), $items_per_page = 0) {

        if(!empty($condition['searchLang'])){
            $lang = $condition['searchLang'];
        }else{
            $lang = "";
        }
        if(!empty($condition['searchCond'])){
            $condition = $condition['searchCond'];
        }else{
            $condition = "";
        }
		$sql = "
			SELECT lo.`id`, lo.`code`, lo.`language_default_id` , ld.`default_text` , lo.`translate_text`, ld.`id` AS languages_other
			FROM `n_languages_default` AS ld
			LEFT JOIN  `n_languages_other` AS lo ON lo.`language_default_id` = ld.`id` AND lo.`code` = '$lang'
		";
		if(!empty($condition)){
			$sql .= " WHERE ".$condition;
		}
		if(count($sort)>0){
			$sql .= " ORDER BY ";
			foreach($sort as $key=>$val){
				$sql .= "`$key` $val";
			}
		}else{
			$sql .= "ld.`default_text` ASC";
		}
		if(!$page) $page = 1;
		$start = ($page-1) * (int)$items_per_page;
		if($start >= 0 && $items_per_page > 0){
			$sql .= " LIMIT $start, $items_per_page";
		}
		$results = $this->_db->query($sql);
		if($results) {
			$objectsInfo = array();
			while($row = $this->_db->fetchArray($results)) {
				$objectInfo = new $this->objectInfo;
				$objectInfo->setField('id', $row['id']);
				$objectInfo->setField('code', $row['code']);
				$objectInfo->setField('language_default_id', $row['language_default_id']);
				$objectInfo->setField('default_text', $row['default_text']);
				$objectInfo->setField('translate_text', $row['translate_text']);
				$objectInfo->setField('languages_other', $row['languages_other']);
				$objectsInfo[] = $objectInfo;
				unset($objectInfo);
			}
			$this->_db->freeResult($results);
			unset($results);
			
			// Count row of query
			$sqlCount = "
				SELECT count(ld.`id`) as numRows
				FROM  `n_languages_default` AS ld
				LEFT JOIN  `n_languages_other` AS lo ON lo.`language_default_id` = ld.`id` AND lo.`code` =  '$lang'
			";
			if(!empty($condition)){
				$sqlCount .= " WHERE ".$condition;
			}
			$results = $this->_db->query($sqlCount);
			$rows = $this->_db->fetchRow();
			$this->_db->freeResult($results);
			
			$arrInfor = array();
			$pages['page']      = $page;
			$pages['pages']     = ceil($rows[0]/$items_per_page);
			$pages['rows']      = $rows[0];
			$pages['start']     = $start;
			$arrInfor['data']   = $objectsInfo;
			$arrInfor['pages']  = $pages;
			return $arrInfor;
		}
		return '';
	}
}
?>